package day03;

import java.util.Scanner;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/10
 */
public class Pi {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入项数");
        int number = sc.nextInt();
        double pi = 0.0;

        for (int i = 1; i <= number; i++) {
            double current = 1.0 / (2 * i - 1); // 计算当前项的值
            if (i % 2 != 0) { // 奇数项为正
                pi += current;
            } else { // 偶数项为负
                pi -= current;
            }
        }

        pi *= 4;

        System.out.println("π使用" + number + "项的近似值为: " + pi);

    }
}
